package org.openkoala.koala.wizard.wizardpage;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.wizards.AbstractMavenWizardPage;
import org.eclipse.m2e.core.ui.internal.wizards.WidthGroup;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.wizard.wizardpage.component.ArtifactComponent;

/**
 * maven相关信息的录入界面（groupId,artifactId等）
 * @author xmfang
 *
 */
@SuppressWarnings("restriction")
public class MavenInfoWizardPage extends AbstractMavenWizardPage {

	protected ArtifactComponent artifactComponent;

	private Project project;
	
	public MavenInfoWizardPage(Project project) {
		super("MavenInfoWizardPage");
		this.project = project;
		setTitle("Project Maven Information");
		setDescription("Input the project maven informations!");
		setPageComplete(false);
	}

	@Override
	public void createControl(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;

		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(layout);

		WidthGroup widthGroup = new WidthGroup();
		container.addControlListener(widthGroup);

		ModifyListener modifyingListener = new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		};

		artifactComponent = new ArtifactComponent(container, SWT.NONE);
		artifactComponent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		artifactComponent.setWidthGroup(widthGroup);
		artifactComponent.setModifyingListener(modifyingListener);

		artifactComponent.setArtifactId(project.getAppName());
		
		addFieldWithHistory("groupId", artifactComponent.getGroupIdCombo());
		addFieldWithHistory("artifactId", artifactComponent.getArtifactIdCombo());
		addFieldWithHistory("version", artifactComponent.getVersionCombo());

		container.layout();
		setControl(container);

		validate();
	}

	public void setVisible(boolean visible) {
		super.setVisible(visible);
		artifactComponent.getGroupIdCombo().setFocus();
	}

	void validate() {
		String error = validateInput();
		setErrorMessage(error);
		setPageComplete(error == null);
	}

	private String validateInput() {
		String error = validateGroupIdInput(artifactComponent.getGroupId().trim());
		if (error != null) {
			return error;
		}

		error = validateArtifactIdInput(artifactComponent.getArtifactId().trim());
		if (error != null) {
			return error;
		}

		if (artifactComponent.getVersion().trim().length() == 0) {
			return Messages.wizardProjectPageMaven2ValidatorVersion;
		}
//
//		if (artifactComponent.getPackaging().trim().length() == 0) {
//			return Messages.wizardProjectPageMaven2ValidatorPackaging;
//		}

		return null;
	}

	/**
	 * Updates the properties when a project name is set on the first page of
	 * the wizard.
	 */
	public void setProjectName(String projectName) {
		if (artifactComponent.getArtifactId().equals(
				artifactComponent.getGroupId())) {
			artifactComponent.setGroupId(projectName);
		}
		artifactComponent.setArtifactId(projectName);
		validate();
	}

	public void refreshArtifactId(String artifactId) {
		if (artifactComponent != null) {
			artifactComponent.setArtifactId(artifactId);
		}
	}
	
	@Override
    public IWizardPage getNextPage() {
		project.setGroupId(artifactComponent.getGroupId());
		project.setArtifactId(artifactComponent.getArtifactId());
		project.setVersion(artifactComponent.getVersion());
		project.setDescription(artifactComponent.getDescription());
//		project.initModulePrefix(project.getAppName());//TODO
//		project.setPackaging(artifactComponent.getPackaging());
		return super.getNextPage();
    }

}
